import {createRouter,createWebHistory,createWebHashHistory} from 'vue-router'

// const Home = () => import('../Views/Home.vue')
// const About = () => import('../Views/About.vue')

const router=createRouter({
    history:createWebHashHistory(),
    routes:[
        {path:'/',redirect:'/home'},
        {
            name:'home',
            path:'/home',
            component:()=>import(/* webpackChunkName: 'home' */"../Views/Home.vue"),
            meta:{
                name:'why',
                age:19
            },
            children:[
                {
                    path:'',
                    redirect:'/home/recommend'
                },
                {
                    path:'recommend',
                    component:()=>import('../Views/HomeRecommend.vue')
                },
                {
                    path:'ranking',
                    component:()=>import('../Views/HomeRanking.vue')
                }
            ]
        },
        {path:'/about',component:()=>import(/* webpackChunkName: 'about' */'../Views/About.vue')},
        {
            path:'/user/:id',
            component:()=>import('../Views/User.vue')
        },
        {
            path:'/login',
            component:()=>import('../Views/Login.vue')
        },
        {
           path:'/order',
           component:()=>import('../Views/Order.vue')
        },
        {
            path:'/:pathMatch(.*)*',
            component:()=>import('../Views/NotFound.vue')
        }
    ]
})

let isAdmin=true
if(isAdmin){
    router.addRoute({
        path:'/admin',
        component:()=>import('../Views/Admin.vue')
    })

    router.addRoute('home',{
        path:'vip',
        component:()=>import('../Views/HomeVip.vue')
    })
}

router.beforeEach((to,from)=>{

    const token=localStorage.getItem('token')
    console.log('to.path:',to.path)
    if(to.path==='/order'&&!token){
        return '/login'
    }
})

export default router